/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */
package com.ykht.proj.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.coupon.entity.CnLabel;
import com.ykht.proj.coupon.mapper.CnLabelMapper;
import com.ykht.proj.coupon.service.CnLabelService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 标签表
 *
 * @author proj code generator
 * @date 2020-11-16 17:00:21
 */
@Service
@RequiredArgsConstructor
@Slf4j
@Transactional
public class CnLabelServiceImpl extends ServiceImpl<CnLabelMapper, CnLabel> implements CnLabelService {

	@Override
	public R insertCnLabel(CnLabel cnLabel) {
		try {
			QueryWrapper<CnLabel> queryWrapper = new QueryWrapper();
			List<CnLabel> list = this.list(queryWrapper.lambda().eq(CnLabel::getName,cnLabel.getName()));
			if(list==null || list.size()==0){
				boolean b = queryValue(cnLabel);
				if(b){
					this.save(cnLabel);
					return R.ok();
				}else{
					return R.failed("数字重复,请重新填写");
				}
			}else{
				return R.failed("标签重复");
			}
		}catch (Exception e){
			e.printStackTrace();
			return R.failed();
		}
	}

	@Override
	public R updateLabel(CnLabel cnLabel) {
		try {
			QueryWrapper<CnLabel> queryWrapper = new QueryWrapper();
			List<CnLabel> list = this.list(queryWrapper.lambda().eq(CnLabel::getName,cnLabel.getName())
					.ne(CnLabel::getId,cnLabel.getId()));
			if(list==null || list.size()==0){
				boolean b = queryValue(cnLabel);
				if(b){
					this.updateById(cnLabel);
					return R.ok();
				}else{
					return R.failed("数字重复,请重新填写");
				}
			}else {
				return R.failed("标签重复");
			}
		}catch (Exception e){
			e.printStackTrace();
			return R.failed();
		}
	}

	private boolean queryValue(CnLabel cnLabel){
		List<CnLabel> list = this.list();
		if(list !=null && list.size()>0){
			for(CnLabel label:list){
				if(label.getValue()!=null && cnLabel.getValue()!=null){
					if(cnLabel.getId()!=null ){
						if(!cnLabel.getId().equals(label.getId())){
							if(label.getValue().equals(cnLabel.getValue())){
								return false;
							}
						}
					}else{
						if(label.getValue().equals(cnLabel.getValue())){
							return false;
						}
					}
				}
			}
		}
		return true;
	}
}
